/*
 ToolBarDelegate.m
 iSpeLL
 
 Created by liulei on 5/30/10.
 Copyright 2010 cool8jay@qq.com. All rights reserved.
 */

#import "ToolBarDelegate.h"

/*
 * All item identifiers are strings; we define the identfier for the search
 * toolbar item as a static string for convenience.
 */
static NSString *ITToolbarSearchItemIdentifier = @"ispell.toolbar.navigator";

@implementation ToolBarDelegate
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar {
    /* The toolbar parameter is ignored in this method, because this
     * delegate is associated with just one toolbar. */
    return [NSArray arrayWithObjects: NSToolbarPrintItemIdentifier,
            NSToolbarShowFontsItemIdentifier,
            NSToolbarShowColorsItemIdentifier,
            NSToolbarSpaceItemIdentifier,
            NSToolbarFlexibleSpaceItemIdentifier,
            NSToolbarSeparatorItemIdentifier,
            NSToolbarCustomizeToolbarItemIdentifier,
            ITToolbarSearchItemIdentifier, nil];// nil is the last item as per API.
}

- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar {
    /* The toolbar parameter is ignored in this method, because this
     * delegate is associated with just one toolbar. */
    return [NSArray arrayWithObjects:NSToolbarFlexibleSpaceItemIdentifier,
            ITToolbarSearchItemIdentifier, nil];// nil is the last item as per API.
}

- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar
     itemForItemIdentifier:(NSString *)itemIdentifier
 willBeInsertedIntoToolbar:(BOOL)flag {
    /*
     * This method only needs to implement the logic for returning the
     * item corresponding to the ITToolbarSearchItemIdentifier, because
     * the other identifiers are handled by the corresponding method
     * defined in NSToolbar. Note that the NSToolbar method is called
     * first.
     *
     * This method ignores the <code>flag</code> parameter, as it does
     * not affect what is returned.
     */
    if ([itemIdentifier isEqual: ITToolbarSearchItemIdentifier]) {
        NSToolbarItem *newToolbarItem = [[NSToolbarItem alloc] 
                                         initWithItemIdentifier:ITToolbarSearchItemIdentifier];
        
        /* Set the required labeling information. In a production
         * application, this would be externalized. */
        [newToolbarItem setLabel:@"Search"];
        [newToolbarItem setPaletteLabel:@"Search"];
        [newToolbarItem setToolTip:@"Search for Packages"];
        
        /*
		 Now set the view that will represent this item, along with
		 its sizing information. 
		 */
        [newToolbarItem setView:searchToolbarItemView];
        CGFloat height = NSHeight([searchToolbarItemView frame]);
        [newToolbarItem setMinSize:NSMakeSize(100, height)];
        [newToolbarItem setMaxSize:NSMakeSize(200, height)];
		
        
        /*
		 No need for autorelease, as we are using Objective-C 2.0,
		 which has garbage collection. 
		 */
        return newToolbarItem;
    } else {
        /*
		 If control gets here, there is nothing to return, because
		 the passed item descriptor is not supported.
         */
        return nil;
    }
}

@end